package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.service.AdminGoodsCommentService;
import com.cskaoyan.market.db.admin.vo.AdminCommentVo;
import com.cskaoyan.market.db.common.BasePageInfo;
import com.cskaoyan.market.db.common.CommonData;
import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketCommentExample;
import com.cskaoyan.market.db.mapper.MarketCommentMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 小焦
 * @since 2023/03/22 13:22
 */
@Service
public class AdminGoodsCommentServiceImpl implements AdminGoodsCommentService {
    /**
     *
     * @param basePageInfo
     * @param userId
     * @param valueId
     */
    @Override
    public CommonData<MarketComment> list(BasePageInfo basePageInfo, String userId, String valueId) {

        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketCommentMapper storageMapper = sqlSession.getMapper(MarketCommentMapper.class);

        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        try {
            if (!StringUtils.isEmpty(userId)) {
                criteria.andUserIdEqualTo(Integer.valueOf(userId.trim()));
            }
            if (!StringUtils.isEmpty(valueId)) {
                criteria.andValueIdEqualTo(Integer.valueOf(valueId.trim()));
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return null;
        }
        criteria.andDeletedEqualTo(false);
        commentExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());

        List<MarketComment> marketComments = storageMapper.selectByExample(commentExample);
        sqlSession.commit();
        sqlSession.close();
        // List<AdminCommentVo> commentVos = ReflectUtils.copyProperties(marketComments, AdminCommentVo.class);
        PageInfo<MarketComment> pageInfo = new PageInfo<>(marketComments);

        CommonData<MarketComment> data = CommonData.data(pageInfo);
        return data;
    }

    @Override
    public int delete(AdminCommentVo commentVo) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketCommentMapper commentMapper = sqlSession.getMapper(MarketCommentMapper.class);
        try {
            commentMapper.logicalDeleteByPrimaryKey(commentVo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return 405;
        }
        sqlSession.commit();
        sqlSession.close();
        return 0;
    }
}
